<?php
/**
 * 21天减重测评算法
 * Created by PhpStorm.
 * User: liao
 * Date: 2016/12/12
 * Time: 16:41
 */

namespace App\Services\Result;
use App\Models\LogHelper;

class BaseResult
{

    protected function compactAttrObj($id,$title,$note,$extra=''){
        return [
            'attr_id' => $id,
            'title' => $title,
            'note' => $note,
            'extra' =>$extra
        ];
    }

    protected function getAnswerObjByIndex($answers,$index){
        $obj = [];
        foreach ($answers as $answer){
            if($index == $answer['index']){
                $obj = $answer;
                break;
            }
        }
        return $obj;
    }


    /**
     * 配置各个指数的属性对虾nag
     */
    protected function configAttrObj($id,$title){
        $obj =  [
            'attr_id' => $id,
            'title' => $title
        ];
        return $obj;
    }


    protected function convertAnswerArr($answers){
        $result = [];
        foreach ($answers as $an){
            $obj[$an['index']] = $an;
            array_push($result,$obj);
        }
        return $result;
    }



    protected function getChatType($type = 'gauge'){
        $types = [
            'radar' => 'radar',
            'gauge' => 'gauge',
        ];
        return $types[$type];
    }


    /**
     * 获取单个题目对应的条件判断值
     */
    protected function getQuestionItemCondResult($expression,$originVal){
        $expression = $expression.'?1:0;';
        $result = $this->compactExpress($expression,$originVal);
        return $result;
    }

    /**
     * 组装表达式,这里充分的利用eval函数可以计算的功能
     * @param string $expression 原始表达式
     * @param string $orgVal 原始值
     * @return string
     */
    protected function compactExpress($expression, $orgVal){
        $val = '$val=$orgVal;$val='.$expression;
        eval($val);
        return $val;
    }



    /**
     * 将answer转换为数组
     * @param $answerStr
     * @return array
     */
    protected function convertAnswer($answerStr){

        $answers = [];
        $answerArr = explode('##',$answerStr);
        if(count($answerArr)>0){
            foreach ($answerArr as $answer){
                $singleArr = explode(':',$answer);
                if(count($singleArr)>0){
                    $obj['index'] = $singleArr[0];
                    $obj['weight'] = $singleArr[1];
                    $obj['answer'] = $singleArr[2];
                    array_push($answers,$obj);
                }
            }
        }
        LogHelper::log('answers====>');
        LogHelper::log($answers);
        return $answers;
    }
}